﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Atomic.Common;
using Atomic.Common.Validators;
using Atomic.Configuration.DAL;

/****************************************************************
 * 作者：杜一博
 * 日期：2012/9/19
 * 描述：
 * 
 * --------------------------------------------------------------
 * 修改记录：
 * --------------------------------------------------------------
 * 修改人：
 * 修改日期：
 * 修改内容：
 * 
 ****************************************************************/

namespace Atomic.Configuration.BLL
{
    /// <summary>
    /// 描述内容
    /// </summary>
    public class ConfigBLL : ErrorImpl, IConfig
    {
        /// <summary>
        /// 验证并添加新配置，如果已经存在则写入提示信息并返回
        /// </summary>
        /// <param name="entity">要保存的配置</param>
        /// <returns>是否保存成功</returns>
        public bool Add(Entities.ConfigEntity entity)
        {
            //实体验证输入
            if (!(VResult = entity.Validate()).Success)
            {
                Error = new ErrorResult()
                {
                    Message = VResult.FailureMessage
                };
                return false;
            }

            ConfigDAL configDAL = new ConfigDAL();
            //如果已经存在，提示并返回
            if (configDAL.Exist(m => m.Key.Equals(entity.Key)))
            {
                Error = new ErrorResult()
                {
                    Message = string.Format("已经存在键名为 {0} 的配置", entity.Key)
                };
                return false;
            }
            //保存到数据库
            configDAL.Add(entity);
            return true;
        }

        /// <summary>
        /// 更新配置信息
        /// </summary>
        /// <param name="entity">要更新的配置信息</param>
        /// <param name="key">配置键名</param>
        /// <returns>是否更新成功</returns>
        public bool Update(Entities.ConfigEntity entity, string key)
        {
            entity.Key = key;   //防止错误，键名矫正

            //实体验证输入
            if (!(VResult = entity.Validate()).Success)
            {
                Error = new ErrorResult()
                {
                    Message = VResult.FailureMessage
                };
                return false;
            }

            ConfigDAL configDAL = new ConfigDAL();
            //更新到数据库
            configDAL.Update(entity, key);
            return true;
        }

        /// <summary>
        /// 删除指定键
        /// </summary>
        /// <param name="key">键名</param>
        /// <returns>是否删除成功</returns>
        public bool Remove(string key)
        {
            ConfigDAL configDAL = new ConfigDAL();
            configDAL.Remove(key);
            return true;
        }

        /// <summary>
        /// 获取指定配置
        /// </summary>
        /// <param name="key">配置名</param>
        /// <returns>配置信息</returns>
        public Entities.ConfigEntity Get(string key)
        {
            ConfigDAL configDAL = new ConfigDAL();
            return configDAL.Get(key);
        }

        /// <summary>
        /// 获取所有满足条件的配置信息
        /// </summary>
        /// <param name="predicate">筛选条件</param>
        /// <returns>满足条件的配置信息</returns>
        public List<Entities.ConfigEntity> GetList(System.Linq.Expressions.Expression<Func<Entities.ConfigEntity, bool>> predicate = null)
        {
            ConfigDAL configDAL = new ConfigDAL();
            return configDAL.GetList(predicate);
        }

        /// <summary>
        /// 分页获取所有满足条件的配置信息
        /// </summary>
        /// <param name="pager">分页参数</param>
        /// <param name="predicate">筛选条件</param>
        /// <returns>分页配置列表</returns>
        public Pager<Entities.ConfigEntity> GetPager(Pager pager, System.Linq.Expressions.Expression<Func<Entities.ConfigEntity, bool>> predicate = null)
        {
            ConfigDAL configDAL = new ConfigDAL();
            return configDAL.GetPager(pager, predicate);
        }
    }
}
